package edu.chinasoft.service;

import edu.chinasoft.domain.Function;
import edu.chinasoft.domain.PageUnitForFunction;
import edu.chinasoft.domain.User;
import edu.chinasoft.mapper.FunctionMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service("functionService")
public class FunctionServiceImp implements FunctionService {
    private static Logger logger = LoggerFactory.getLogger(FunctionServiceImp.class);
    @Autowired
    @Qualifier(value = "functionMapper")
    private FunctionMapper functionMapper;
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    @Override
    public List<Function> selectFunctionByUserID(User user){
        logger.info("FunctionServiceImp>>>selectFunctionByUserID start ……");
        List<Function> functionList = functionMapper.selectFunctionByUserID(user);
        logger.info("FunctionServiceImp>>>selectFunctionByUserID end ……");
        return functionList;
    }
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
	@Override
	public List<Function> selectFunction() {
		// TODO Auto-generated method stub
		logger.info("FunctionServiceImp>>>selectFunction start ……");
		List<Function> functionList = functionMapper.selectFunction();
		logger.info("FunctionServiceImp>>>selectFunction end ……");
		return functionList;
	}
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
	@Override
	public PageUnitForFunction getRecordNum() {
    	logger.info("FunctionServiceImp>>>getRecordNum start ……");
    	PageUnitForFunction recordNum = functionMapper.getRecordNum();
		logger.info("FunctionServiceImp>>>getRecordNum end ……");
			return recordNum;
	}
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
	@Override
	public List<Function> selectUnitFunction(Map<String, Object> map) {
		logger.info("FunctionServiceImp>>>selectUnitFunction start ……");
		List<Function> functionList = functionMapper.selectUnitFunction(map);
		logger.info("FunctionServiceImp>>>selectUnitFunction end ……");
		return functionList;
	}
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
	@Override
	public boolean insertFunction(Function function) {
		logger.info("FunctionServiceImp>>>insertFunction start ……");
		boolean result=functionMapper.insertFunction(function);
		logger.info("FunctionServiceImp>>>insertFunction end ……");
		return false;
	}
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
	@Override
	public Function selectFunctionById(Function function) {
		logger.info("FunctionServiceImp>>>selectFunctionById start ……");
		 Integer fatherFunctionById = functionMapper.selectFatherFunctionById(function);
		if(fatherFunctionById==null) {//没有父权限
			function = functionMapper.selectFaFunctionById(function);
		}else {
			function = functionMapper.selectSonFunctionById(function);
		}
		logger.info("FunctionServiceImp>>>selectFunctionById end ……");
		return function;
	}
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
	@Override
	public boolean updateFunctionByFunctionId(Function function) {
		logger.info("FunctionServiceImp>>>updateFunctionByFunctionId start ……");
		logger.info("父权限的名字为："+function.getFatherFunction().getFunctionName());
		Integer fatherId = functionMapper.selectFatherFunctionByName(function);
		logger.info("父权限的Id为："+fatherId);
		function.getFatherFunction().setFunctionId(fatherId);
		boolean result = functionMapper.updateFunctionByFunctionId(function);
		logger.info("FunctionServiceImp>>>updateFunctionByFunctionId end ……");
		return result;
	}
    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
	@Override
	public boolean deleteFunctionById(Function function) {
		logger.info("FunctionServiceImp>>>deleteFunctionById start ……");
		Integer exitFather = functionMapper.selectFatherFunctionById(function);
		//先删除t_role_function再删除function
		boolean delete_roleFunction = false;
        boolean result_function = false;
        int flag_function = 0;
        int flag_roleFunction = 0;
        int sum_roleFunction = 0;
        if(exitFather==null) {
        	//表示当前权限为父权限 删除自己与子权限与role_functio
        	//获取当前父权限的子权限
        	List<Function> functionList = functionMapper.selectFunctionByFatherId(function);
        	//把父权限也加进列表里删除
        	functionList.add(function);
        	for(Function element:functionList) {
        		int num = functionMapper.selectRoleFunctionNum(element);
        		sum_roleFunction+=num;
        		if(num!=0) {
        			delete_roleFunction=functionMapper.deleteRoleFunction(element);
        			if(delete_roleFunction) {
        				flag_roleFunction++;
        			}
        		}
        		result_function = functionMapper.deleteFunctionById(element);
        		if(result_function) {
        			flag_function++;
        		}
        	}
        	if(flag_function==functionList.size()) {
        		result_function=true;
        	}
        }else {//子权限删除自己与role_function
        	delete_roleFunction=functionMapper.deleteRoleFunction(function);
        	result_function=functionMapper.deleteFunctionById(function);
        }
		logger.info("FunctionServiceImp>>>deleteFunctionById end ……");
		return delete_roleFunction&&result_function;
	}
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
	@Override
	public List<Function> selectFatherFunction() {
		// TODO Auto-generated method stub
    	logger.info("FunctionServiceImp>>>selectFatherFunction start ……");
    	List<Function> selectFatherFunction = functionMapper.selectFatherFunction();
    	logger.info("FunctionServiceImp>>>selectFatherFunction end ……");
		return selectFatherFunction;
	}

    
}
